package nio;

import nio.util.BaseUtil;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Nio文件写入写出
 */
public class NioDemo2 {

    public static void main(String[] args) throws Exception{

        FileChannel in = new FileInputStream("niodemo1.txt").getChannel();
        FileChannel out = new FileOutputStream("niodemo2.txt").getChannel();

        ByteBuffer buffer = ByteBuffer.allocateDirect(7);
        BaseUtil.logBufferInfo(buffer,"init");

        while (in.read(buffer)!=-1){
            BaseUtil.logBufferInfo(buffer,"read");
            buffer.flip();
            BaseUtil.logBufferInfo(buffer,"flip");

            out.write(buffer);
            BaseUtil.logBufferInfo(buffer,"write");
            buffer.clear();
            BaseUtil.logBufferInfo(buffer,"clear");
        }

        in.close();
        out.close();
    }
}
